<?php
/**
 * Created by PhpStorm.
 * User: wangsentao
 * Date: 2018/3/2
 * Time: 上午11:26
 */

namespace app\common\Model;

require_once dirname(dirname(__DIR__)) . '/common/Ext/Alipay/AopSdk.php';
use app\api\service\JpushService;

class AliNotifyModel
{
    public function verifyPay($params)
    {
        $conf = config('pay')['ALI_PAY'];

        $aop = new \AopClient();
        $aop->alipayrsaPublicKey = $conf['ali_public_key'];

        $sign = $aop->rsaCheckV1($params, null, 'RSA');
        //验证签名
        if ($sign == false) {
            return false;
        }

        $order_num = $params['out_trade_no'];//商户订单号
        $trade_no = $params['trade_no']; //支付宝交易号
        $trade_status = $params['trade_status'];//交易状态
        $total_amount = $params['total_fee'];//订单实际金额

        if ($trade_status != 'TRADE_FINISHED' && $trade_status != 'TRADE_SUCCESS') {
            //支付交易状态不对
            return false;
        }
        $sys_orderinfo = Order::getOrderInfoByOrderNumOrId($order_num, '');

        if ($sys_orderinfo['code'] == 'success' && $sys_orderinfo['data']['order_type'] == 1) {
            //极光推送订单
            JpushService::instance()->makeOrder($sys_orderinfo['data']['order_id']);
            $res = Order::updateShaoHuoOrder($sys_orderinfo['data']['uid'], $params['out_trade_no'], $sys_orderinfo['data']['order_id'], $params['trade_no']);
            if ($res['code'] == 'success') {
                return true;
            }
            return false;
        }
        //更新充值流水等
        if ($sys_orderinfo['code'] == 'success' && $sys_orderinfo['data']['order_type'] == 4) {//充值
            $res = Order::updateRechargeOrder($sys_orderinfo['data']['uid'], $params['out_trade_no'], $params['trade_no'], $sys_orderinfo['data']['amount']);
            if ($res['code'] == 'success') {
                return true;
            }
            return false;
        }
    }
}